package cn.loioi.web.components.spring.result;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

/**
 * <br>
 * (c) Copyright 2016 by <br>
 * @description :数据返回实体类 <br>
 * @file_name :Result.java <br>
 * @system_name :car-client <br>
 * @author :yyzh <br>
 * @创建时间 :2016年4月15日 下午5:31:00
 */
public class Result<T extends Result<?>> implements Serializable {
	private static final long serialVersionUID = -3868460256524363738L;
	private ResultError status;

	public Result() {
		this(new ResultError());
	}

	public Result(ResultError error) {
		super();
		this.status = error;
	}

	@SuppressWarnings("unchecked")
	public T setResultNo(int code) {
		this.status.setCode(code);
		return (T) this;
	}

	@SuppressWarnings("unchecked")
	public T setResultText(String msg) {
		this.status.setMsg(msg);
		return (T) this;
	}

	@SuppressWarnings("unchecked")
	public T setError(int code, String msg) {
		this.status.setCode(code);
		this.status.setMsg(msg);
		return (T) this;
	}

	@JSONField(name = "success")
	public boolean getSuccess() {
		return status.getCode() == 1;
	}

	@JSONField(name = "code")
	public int getCode() {
		return status.getCode();
	}

	@JSONField(name = "msg")
	public String getMsg() {
		return status.getMsg();
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toString() {
		return JSON.toJSONString((T) this);
	}

}
